/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/user/User.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: 'Login' },
    { path: '/Login', component: Login },
    { path: '/Home',
      component: Home,
      redirect: '/Welcome',
      children: [
        { path: '/Welcome', component: Welcome }, { path: '/users', component: Users } ]
    }
  ]
})

// 挂在路由导航卫士

// 每个页面都守卫，按需守卫

// 路由meta信息 requireAuth（需要权限） 页面才守卫

router.beforeEach((to, from, next) => {
  // to 将要访问的路由

  // from代表从哪个路径跳转而来

  // next是一个函数,表示是否方形

  // next() 放行   next('路径')强制跳转到的位置

  if (to.path === '/login') return next()

  // 获取token

  const tokenStr = window.sessionStorage.getItem('token')

  // 取反tokenstr ,不存在那么强制跳转到登录页

  if (!tokenStr) return next('/login')

  // 存在则next()通过

  next()
})

export default router
